#coding=utf-8
#LightCam Mongo数据清理脚本
#init by hfh 20150305

import multiprocessing,subprocess,threading
import socket,os,time,re,traceback,sys
import lc_libs

def func(this):

	dbname=this['dbname']
	tablename=this['tablename']
	keeptime=this['keeptime']
	interval=this['interval']
	field=this['field']

	print tablename
	
	try:
		#使用数据库
		operdb=db.useDatabase(dbname)
		opertable=db.UseCollection(operdb,tablename)

		#数据库更新
		opertime=int(time.time())-int(keeptime)
		expr={'$lt':opertime}
		exprcont={field:expr}

		db.deleteData(opertable,exprcont)

	except Exception:
		excinfo=traceback.format_exc()
		logdate=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
		logfile.write(logdate+" :\n"+excinfo+"\n")
		logfile.flush()
		time.sleep(1)


if __name__ == "__main__":

	lchome=lc_libs.lc_getlchome()
	scname=sys.argv[0].split('.')[0]
	logfilename=lchome+'/log/'+scname+'.log'
	confname=lchome+'/conf/'+scname+'.conf'
	
	today=time.strftime('%Y%m%d',time.localtime())
	os.system("mv %s %s.%s" % (logfilename,logfilename,today))
	
	logfile=file(logfilename,'a')
	logdate=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
	logfile.write(logdate+" Started.\n")
	logfile.flush()
	
	agent_hostname=lc_libs.lc_gethostname()
	
	#pool = multiprocessing.Pool(processes=1)
	#result = []

	try:
		#初始化数据库
		
		db=lc_libs.lc_mongodb_operater('127.0.0.1',27017)

		db.connect()
		Conn=db.getConn()
		if Conn==None:
			print "Can not Connect to the MondbDB"
			exit(0)

	except Exception:
		excinfo=traceback.format_exc()
		logdate=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
		logfile.write(logdate+" 初始化数据库失败:\n"+excinfo+"\n")
		logfile.flush()
		exit(0)

	#主程序
	MonitorList = []
	
	for lines in open(confname):
		if lines[0] == '#' or lines.split("\t")[0] not in (agent_hostname,'default'):
			continue
		line=lines.strip('\r\n').split('\t')
		dconf={}
		
		#dconf['agent_hostname']=line[0]
		dconf['dbname']=line[1]
		dconf['tablename']=line[2]
		dconf['keeptime']=lc_libs.lc_returnseconds(line[3])
		dconf['interval']=lc_libs.lc_returnseconds(line[4])
		dconf['field']=line[5]
	
		MonitorList.append(dconf)

	usedtime=0
	
	while True:
		time.sleep(1)
		realtime=int(time.time())
		
		logdate=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
		#holiday_state = lc_libs.lc_isholiday()
		
		threads = []
		for i in MonitorList:
			#if int(i['holiday']) == 1 and holiday_state == True:
			#	continue
				
			#time_state = lc_libs.lc_schedule(i['schedule'])
			#if time_state == False:
			#	continue
			
			flag=realtime%int(i['interval'])
			#print flag
			if flag<=usedtime:
				#print i
				t = threading.Thread(target=func,args=(i,))
				threads.append(t)
		
		for t in threads:
			t.start()
			while True:
				#print len(threading.enumerate())
				time.sleep(0.01)
				if(len(threading.enumerate()) < 100):
					break

		#排除执行程序导致的时间差
		usedtime=int(time.time())-realtime
		#print "usedtime:%s" % usedtime
